package com.zzu.team.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzu.team.entity.ReturnBean;
import com.zzu.team.entity.Tester;
import com.zzu.team.service.TesterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参加测试的用户(Tester)表控制层
 *
 * @author makejava
 * @since 2021-11-27 20:11:32
 */
@RestController
@RequestMapping("tester")
public class TesterController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TesterService testerService;

    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param tester 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public ReturnBean selectAll(Integer page, Integer limit,Tester tester) {

        Page<Tester> pageTester = new Page<>(page, limit);
        Page<Tester> page1 = testerService.page(pageTester, new QueryWrapper<>(tester));


        return super.success(page1.getRecords(), page1.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Tester selectById(@PathVariable Serializable id) {
        Tester tester = testerService.getById(id);
        return tester;
    }


    /**
     * 根据用户名和手机号查询
     * @param tName 用户名
     * @param tPhone 手机号
     * @return 返回查询结果
     */
    @GetMapping("selectByNameAndPhone")
    public ReturnBean selectByNameAndPhone(@RequestParam("name") String tName, @RequestParam("phone") String tPhone){
        Map<String, Object> map = new HashMap<>();
        // 先根据手机号查询
        map.put("testPhone", tPhone);
        List<Tester> testers = testerService.listByMap(map);
        // 如果手机号存在 testers不为空
        if (testers.size()>0) {
            // 再根据用户名查询
            map.put("testName", tName);
            List<Tester> testers2 = testerService.listByMap(map);
            // 如果此时testers2为空，说明用户名错误
            if (testers2.size()==0) return fail("用户名错误");
            // 否则查询成功
            else return success(testers2.get(0));
        }
        // 如果手机号不存在, 添加该用户
        else {
            Tester tester = new Tester();
            tester.setTestname(tName);
            tester.setTestphone(tPhone);
            tester.setTesttime(new Date());
            return insert(tester);
        }
    }

    /**
     * 新增数据
     *
     * @param tester 实体对象
     * @return 新增结果
     */
//    @PostMapping("insert")
    public ReturnBean insert(Tester tester) {
        boolean save = testerService.save(tester);
        if (save) return super.success(tester);
        return fail(tester);
    }

    /**
     * 修改数据
     *
     * @param tester 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public ReturnBean update(@RequestBody Tester tester) {
        boolean update = testerService.updateById(tester);
        if (update) return super.success(tester);

        return fail(tester);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("deleteByIdList")
    public ReturnBean delete(@RequestParam("idList") List<Long> idList) {
        boolean delete = testerService.removeByIds(idList);
        if (delete) return success(idList);
        return fail(idList);
    }

    @DeleteMapping("deleteById")
    public ReturnBean delete(@RequestParam("id")Integer id) {
        boolean delete = testerService.removeById(id);
        if (delete) return success("删除成功");
        return fail("未删除成功");
    }
}

